#include "header.h"
#include <fstream>
#include <sstream>
#include <iomanip>

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

using namespace std;

void createForEachRun(double nRun, int NEachTempRun, const string &directoryName)
{
  int numRun;
  char buf [4];

  if (floor(nRun)+0.5 < nRun)
  {
    numRun = static_cast<int>(floor(nRun) + 1);
  }
  else
  {
    numRun = static_cast<int>(floor(nRun));
  }

  ifstream inA( ("../HMC_core/output/" + directoryName + "/HMC_Solution.out").c_str());

  string lineReadA;

  getline(inA,lineReadA);
  getline(inA,lineReadA);
  getline(inA,lineReadA);
  getline(inA,lineReadA);
  getline(inA,lineReadA);
  getline(inA,lineReadA);
  getline(inA,lineReadA);
  getline(inA,lineReadA);
  getline(inA,lineReadA);

  for (int i = 1; i <= numRun; i++)
  {
    if(fmod((double) i, 10.0) == 0.0)
    {
      ifstream in( ("../HMC_core/output/" + directoryName + "/HMC_diagnosis" + itoa(i,buf,10) + ".out").c_str());

      if (!in)
      {
        cout << "Can't open file\n";
        exit(1);
      }


      ofstream out( ("../HMC_core/output/" + directoryName + "/HMC_diagnosis" + itoa(i,buf,10) + ".txt").c_str());

      string lineRead;

      getline(in, lineRead);
      getline(in, lineRead);

      int someInteger, numEval, numEvalA;
      double someDouble1, someDouble2, someDouble3, funcValue, funcValueA, distance, distanceA;

      while ( getline(in, lineRead, '\n') )
      {
        istringstream dummy(lineRead);

        dummy >> someInteger >> someDouble1 >> someDouble2 >> funcValue >> someDouble3 >> numEval >> distance;

        out << setw(4) << numEval << setw(15) << funcValue << setw(15) << distance << endl;

      }

      in.close();
      out.close();

      ofstream outA( 
        ("../HMC_core/output/" + directoryName + "/HMC_diagnosis" + itoa(i,buf,10) + ".txt").c_str(),
        ios::out | ios::app );

      if(!outA)
      {
        cout << "can't open file\n";
      }

      istringstream dummyA(lineReadA);

      dummyA >> someDouble1 >> funcValueA >> numEvalA >> distanceA;

      outA << setw(4) << numEvalA << setw(15) << funcValueA << setw(15) << distanceA << endl;

      getline(inA,lineReadA);
      getline(inA,lineReadA);
      getline(inA,lineReadA);
      getline(inA,lineReadA);
      getline(inA,lineReadA);
      getline(inA,lineReadA);
      getline(inA,lineReadA);
      getline(inA,lineReadA);

      outA.close();

      cout << "HMC_diagnosis" << i << ".txt has been created..." << endl;
    }
    else
    {
      getline(inA,lineReadA);
    }
  }
  
  inA.close();

  cout << "\nAll HMC_diagnosis.txt files have been written to folder " << directoryName << endl;

}

void createForEachRun(int nRun, const string &directoryName)
{
  int numRun;
  numRun = nRun;
  char buf [4];

  ifstream inA( ("../HMC_core/output/" + directoryName + "/CTS_Solution.out").c_str());

  string lineReadA;

  getline(inA,lineReadA);
  getline(inA,lineReadA);
  getline(inA,lineReadA);
  getline(inA,lineReadA);
  getline(inA,lineReadA);
  getline(inA,lineReadA);
  getline(inA,lineReadA);

  for (int i = 1; i <= numRun; i++)
  {
    if(fmod((double) i, 10.0) == 0.0)
    {
      ifstream in( ("../HMC_core/output/" + directoryName + "/CTS_diagnosis" + itoa(i,buf,10) + ".out").c_str());

      if (!in)
      {
        cout << "Can't open file\n";
        exit(1);
      }

      ofstream out( ("../HMC_core/output/" + directoryName + "/CTS_diagnosis" + itoa(i,buf,10) + ".txt").c_str());

      string lineRead;

      getline(in, lineRead);
      getline(in, lineRead);
      getline(in, lineRead);
      getline(in, lineRead);

      int someInteger, numEval, numEvalA;
      double someDouble1, someDouble2, someDouble3, funcValue, funcValueA, distance, distanceA;

      while ( getline(in, lineRead, '\n') )
      {
        istringstream dummy(lineRead);

        if ( lineRead.compare(45,20, "                    ") )
        {
          dummy >> someInteger >> someDouble1 >> someDouble2 >> funcValue >> numEval >> distance;

          out << setw(4) << numEval << setw(15) << funcValue << setw(15) << distance << endl;
        }
        else
        {
          dummy >> someInteger >> someDouble1 >> funcValue >> numEval >> distance;

          out << setw(4) << numEval << setw(15) << funcValue << setw(15) << distance << endl;
        }
      }

      in.close();
      out.close();

      ofstream outA( 
        ("../HMC_core/output/" + directoryName + "/CTS_diagnosis" + itoa(i,buf,10) + ".txt").c_str(),
        ios::out | ios::app );

      istringstream dummyA(lineReadA);

      dummyA >> someDouble1 >> funcValueA >> numEvalA >> distanceA;

      outA << setw(4) << numEvalA << setw(15) << funcValueA << setw(15) << distanceA << endl;

      getline(inA,lineReadA);

      outA.close();

      cout << "CTS_diagnosis" << i << ".txt has been created..." << endl;
    }

  }

  inA.close();

  cout << "\nAll CTS_diagnosis.txt files have been written to folder " << directoryName << endl;

}